import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:flutterapp/Base/DemoUI/PageMain.dart';
import 'package:flutterapp/Base/Util/NavigatorObserver.dart';
import 'package:flutterapp/Base/PageTest.dart';
import 'package:flutterapp/Base/Util/ShakeManager.dart';
import 'package:flutterapp/Base/style/CusStyle.dart';
import 'package:flutterapp/JpushManager.dart';
import 'file:///C:/Users/41784/AndroidStudioProjects/remoteCtrl/flutter_app/lib/Page/PageDirectiveEditor.dart';
import 'file:///C:/Users/41784/AndroidStudioProjects/remoteCtrl/flutter_app/lib/Page/PageHome.dart';
import 'file:///C:/Users/41784/AndroidStudioProjects/remoteCtrl/flutter_app/lib/Page/PageReg.dart';
import 'package:flutterapp/SaasManager.dart';
import 'package:flutterapp/UniqueIDUtil.dart';
import 'Base/Global.dart';
import 'Base/PageErr.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  Global.initApp().then((_) async {
    runApp(new Application());
    JpushManager.init();
    if (UniqueIDUtil.deviceID != "") {
      JpushManager.autoLogin(UniqueIDUtil.deviceID);
    }
    SaasManager.init();
  }).catchError((e) {
    print("===runApp fialed===  err=>$e");
  });
}

class Application extends StatefulWidget {
  @override
  _ApplicationState createState() => _ApplicationState();
}

class _ApplicationState extends State<Application> {
  var routeMap = {
//    "/": (context) => PageTest(),
    "/": (context) => PageReg(),
//    PageMain.ROUTER_NAME: (context) => PageMain(),
    PageReg.ROUTER_NAME: (context) => PageReg(),
    PageHome.ROUTER_NAME: (context) => PageHome(),
    PageDirectiveEditor.ROUTER_NAME:(context) => PageDirectiveEditor(),
  };

  @override
  void initState() {
    super.initState();
    if (Global.ENV == PUBLISHING_ENVIRONMENT.DEV) {
      ShakeManager.getInstance().startListening();
    }
  }

  @override
  void dispose() {
    if (Global.ENV == PUBLISHING_ENVIRONMENT.DEV) {
      ShakeManager.getInstance().stopListening();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return StoreProvider(
      store: Global.appStore,
      child: StoreBuilder(
        builder: (context, store) {
          return FlutterEasyLoading(
            child: MaterialApp(
                title: "flutterDemo",
                initialRoute: "/",
                routes: routeMap,
                theme: ThemeData(
//                  brightness: Brightness.dark,
                  primaryColor: CusColor.PRIMARY_COLOR,
                  accentColor: CusColor.SECOND_COLOR,
                ),
                navigatorObservers: [MyNavigatorObserver.getInstance()],
                onUnknownRoute: (settings) {
                  return PageRouteBuilder(pageBuilder: (context, __, ___) {
                    return PageErr();
                  });
                }),
          );
        },
      ),
    );
  }
}
